﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;

public partial class Administrator_StatisticProductSales : System.Web.UI.Page
{
    Dataprocess data = new Dataprocess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          //  txtdate.Text = DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString("0000");
         //   txtdate1.Text = DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString("0000");

            LoadData();

        }
    }

    public void LoadData()
    {

        //rptthongke.DataSource = table;
        rptthongke.DataSource = LoadTotalProduct();
        rptthongke.DataBind();

        CollectionPager1.DataSource = data.allSystem("select  b.Code ,b.Name as TenSanPham,a.Name as TenNhom,c.Quantity,c.UnitPrice,c.Date from tblProductCategory a , tblProduct b , tblImExport c where a.Id = b.ProductCategoryID and b.Code = c.ProductCode order by c.Date desc").DefaultView;
        CollectionPager1.PageSize = 40;
        CollectionPager1.BindToControl = rptData;
        rptData.DataSource = CollectionPager1.DataSourcePaged;
        rptData.DataBind();

    }

    private DataTable LoadTotalProduct()
    {
        long tongsl = 0;
        long tongtien = 0;
        DataTable table = new DataTable();
        DataColumn dtColumn = new DataColumn("Ten");
        table.Columns.Add(dtColumn);
        DataColumn dtColumn1 = new DataColumn("Sl");
        table.Columns.Add(dtColumn1);
        DataColumn dtColumn2 = new DataColumn("Gia");
        table.Columns.Add(dtColumn2);

        DataTable dbcatename = data.allSystem("select * from tblProductCategory order by Id");
        foreach (DataRow drcate in dbcatename.Rows)
        {
            long quantity = 0;
            long price = 0;

            int idcate = Convert.ToInt32(drcate["Id"].ToString());
            string name = drcate["Name"].ToString();
            DataTable dbpro = data.allSystem("select a.Name as Nhomsp , b.Name as SanPham ,c.Quantity,c.UnitPrice  from tblProductCategory a , tblProduct b , tblImExport c where a.Id=b.ProductCategoryID and b.Code=c.ProductCode and a.Id=" + idcate + " and c.Action='EXP'  ");
            foreach (DataRow dr in dbpro.Rows)
            {

                long gia = Convert.ToInt64(dr["UnitPrice"].ToString());
                long quan = Convert.ToInt64(dr["Quantity"].ToString());

                quantity += quan;
                price += gia * quan;
            }
            tongsl += quantity;
            tongtien += price;
            DataRow dtRow = table.NewRow();
            dtRow[0] = name;
            dtRow[1] = quantity;
            dtRow[2] = String.Format("{0:0,0}", price);
            table.Rows.Add(dtRow);
        }
        lblsl.Text = "" + tongsl;
        lblgia.Text = String.Format("{0:0,0}", tongtien);
        return table;
    }
    private DataTable LoadTotalProduct(string date, string date1)
    {
        long tongsl = 0;
        long tongtien = 0;

        DataTable table = new DataTable();
        DataColumn dtColumn = new DataColumn("Ten");
        table.Columns.Add(dtColumn);
        DataColumn dtColumn1 = new DataColumn("Sl");
        table.Columns.Add(dtColumn1);
        DataColumn dtColumn2 = new DataColumn("Gia");
        table.Columns.Add(dtColumn2);

        DataTable dbcatename = data.allSystem("select * from tblProductCategory order by Id");
        foreach (DataRow drcate in dbcatename.Rows)
        {
            long quantity = 0;
            long price = 0;

            int idcate = Convert.ToInt32(drcate["Id"].ToString());
            string name = drcate["Name"].ToString();
            DataTable dbpro = data.allSystem("select a.Name as Nhomsp , b.Name as SanPham ,c.Quantity,c.UnitPrice  from tblProductCategory a , tblProduct b , tblImExport c where a.Id=b.ProductCategoryID and b.Code=c.ProductCode and a.Id=" + idcate + " and c.Action='EXP' and c.Date>='" + date + "' and c.Date<='" + date1 + "'  ");
            foreach (DataRow dr in dbpro.Rows)
            {

                long gia = Convert.ToInt64(dr["UnitPrice"].ToString());
                long quan = Convert.ToInt64(dr["Quantity"].ToString());

                quantity += quan;
                price += gia * quan;
            }
            tongsl += quantity;
            tongtien += price;
            DataRow dtRow = table.NewRow();
            dtRow[0] = name;
            dtRow[1] = quantity;
            dtRow[2] = String.Format("{0:0,0}", price);
            table.Rows.Add(dtRow);
        }
        lblsl.Text = "" + tongsl;
        lblgia.Text = String.Format("{0:0,0}", tongtien);
        return table;
    }

    protected void btnAdnew1_Click(object sender, EventArgs e)
    {
        Response.Redirect("BannerAdd.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpdateDisplayOrder_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void btnthongke_Click(object sender, EventArgs e)
    {

        string date = txtdate.Text.Substring(3, 2) + "/" + txtdate.Text.Substring(0, 2) + "/" + txtdate.Text.Substring(6, 4) + " 00:00";
        string date1 = txtdate1.Text.Substring(3, 2) + "/" + txtdate1.Text.Substring(0, 2) + "/" + txtdate1.Text.Substring(6, 4) + " 23:59";


        CollectionPager1.DataSource = data.allSystem("select  b.Code ,b.Name as TenSanPham,a.Name as TenNhom,c.Quantity,c.UnitPrice,c.Date from tblProductCategory a , tblProduct b , tblImExport c where a.Id = b.ProductCategoryID and b.Code = c.ProductCode and c.Date>='" + date + "' and c.Date<='" + date1 + "' order by c.Date desc ").DefaultView;
        CollectionPager1.PageSize = 1000;
        CollectionPager1.BindToControl = rptData;
        rptData.DataSource = CollectionPager1.DataSourcePaged;
        rptData.DataBind();

        rptthongke.DataSource = LoadTotalProduct(date, date1);
        rptthongke.DataBind();
    }
}